package OS.experiment12;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonListener2 implements ActionListener {

    static int CREATE_RESULT = 6;
    static int BLOCK_RESULT = 7;
    static int WAKEUP_RESULT = 8;
    static int STOP_RESULT = 9;
    static int SHOW_RESULT = 10;
    static int SCHEDULE_RESULT_1 = 11;  //时间片轮转调度
    static int SCHEDULE_RESULT_2 = 12;  //优先级调度
    static int SCHEDULE_RESULT = 13;    //调度底部确定按钮

    private JTextField name, priority;
    private int type;
    private Process process;
    private Callback callback;

    /**
     * 构造函数
     * @param name      进程名输入框
     * @param priority  进程优先级输入框
     * @param type      操作类型
     * @param process   操作的系统
     * @param callback  点击回调
     */
    ButtonListener2(JTextField name, JTextField priority, int type, Process process, Callback callback){
        this.name = name;
        this.priority = priority;
        this.type = type;
        this.process = process;
        this.callback = callback;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (type){
            case 6:
                createResult();
                break;
            case 7:
                blockResult();
                break;
            case 8:
                wakeupResult();
                break;
            case 9:
                stopResult();
                break;
            case 10:
            case 13:
                callback.schedule();
                break;
            case 11:
                timeRotation();
                break;
            case 12:
                prioritySchedule();
        }
    }

    private void createResult(){
        PCB nowResult = null;
        try{
            nowResult = process.create(name.getText(), Integer.parseInt(priority.getText()), null);
        }catch (Exception e){
            System.out.println(e);
        }
        StringBuilder builder = new StringBuilder();
        if (null == nowResult){
            builder.append("创建进程失败");
        }else {
            builder.append("进程创建成功");
        }
        toast(builder.toString());
    }

    private void blockResult(){
        int result = -1;
        try{
            result = process.block(name.getText());
        }catch (Exception e){
            System.out.println(e);
        }
        StringBuilder builder = new StringBuilder();
        switch(result) {
            case -1:
                builder.append("进程不存在！");
                break;
            case 0:
                builder.append("当前进程为就绪状态无法阻塞！");
                break;
            case 1:
                builder.append("阻塞成功！");
                break;
            case 2:
                builder.append("当前进程为阻塞状态无法阻塞！");
        }
        toast(builder.toString());
    }

    private void wakeupResult(){
        int result = -1;
        try{//捕获问题来解决解析字符串失败时不显示窗口的问题
            result = process.wakeup(name.getText());
        }catch (Exception e){
            System.out.println(e);
        }
        StringBuilder builder = new StringBuilder();
        switch(result) {
            case -1:
                builder.append("阻塞队列为空或进程不存在！");
                break;
            case 0:
                builder.append("当前进程为就绪状态无法唤醒！");
                break;
            case 1:
                builder.append("当前进程为运行状态无法唤醒！");
                break;
            case 2:
                builder.append("已经唤醒！");
        }
        toast(builder.toString());
    }

    private void stopResult(){
        int result = -1;
        try{
            result = process.stop(name.getText());
        }catch (Exception e){
            System.out.println(e);
        }
        StringBuilder builder = new StringBuilder();
        if (result == -1) {
            builder.append("进程不存在！");
        } else {
            builder.append("已经停止！");
        }
        toast(builder.toString());
    }

    /**
     * 弹窗对话框
     * @param text  提示文字
     */
    private void toast(String text){
        JFrame jFrame = new JFrame();
        jFrame.setSize(300, 200);
        jFrame.setDefaultCloseOperation(3);
        jFrame.setLocationRelativeTo(null);

        GridLayout layout = new GridLayout();
        jFrame.setLayout(layout);
        JLabel toastLabel = new JLabel(text);
        JButton jButton = new JButton("确定");

        jFrame.add(toastLabel);
        jFrame.add(jButton);

        jFrame.setVisible(true);

        //通知界面点击按钮传入监听，回调CloseCallback#close()方法来进行监听何时关闭创建窗口
        ToastListener listener = new ToastListener(callback, new Callback(){
            @Override
            public void schedule() {
                jFrame.setVisible(false);
            }
        });
        jButton.addActionListener(listener);
    }

    /**
     * 时间片轮转操作
     */
    private void timeRotation(){
        process.timeRotation(new Callback() {
            @Override
            public void schedule() {
                //回调来在界面显示进程信息
                callback.schedule();
            }
        });
    }

    /**
     * 优先级调度
     */
    private void prioritySchedule(){
        process.priorityScheduling(new Callback() {
            @Override
            public void schedule() {
                //回调来在界面显示进程信息
                callback.schedule();
            }
        });
    }
}
